<template>
  <div class="body">
    <div class="top">
      <div class="back" @click="back"><i class="el-icon-back"></i></div>
      <div>全部MV</div>
    </div>

    <div class="tagBox">
      <div>最近更新:今天</div>
      <div class="tag">
        <div v-for="(item, i) in areaData" :key="item.id" @click="changeTag(item)"
          :style="id === i ? 'color:#ec4141' : 'color:#000'">
          {{ item.name }}
        </div>
      </div>
    </div>
    <div class="content" v-if="data !== null">
      <div v-for="(item, i) in data" :key="item.id" class="contentBox">
        <div class="rank">
          <div>{{ i + 1 >= 10 ? i + 1 : "0" + (i + 1) }}</div>
          <div>
            <i :style="i + 1 <= item.lastRank ? 'color:red' : 'color:green'"
              :class="i + 1 <= item.lastRank ? 'el-icon-top' : 'el-icon-bottom'"></i>
          </div>
        </div>
        <div class="card">
          <div class="count">
            <i class="el-icon-video-play"></i>{{
                item.playCount >= 10000
                  ? (item.playCount / 10000).toFixed(0) + "万"
                  : item.playCount
            }}
          </div>

          <el-image :src="item.cover" lazy fit="contain" @click="playMv(item)">
            <div slot="placeholder" class="image-slot">
              <el-image src="" lazy @click="playMv(item)"></el-image>
            </div>
          </el-image>
        </div>
        <div class="desc">
          <div class="name">
            {{ item.name }}
          </div>
          <div class="artistName">
            {{ item.artistName }}
          </div>
          <div class="oneword">
            {{ item.mv.oneword }}
          </div>
        </div>
      </div>
    </div>
    <div class="loading" v-if="show === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  mounted() {
    this.getData();
  },
  beforeRouteLeave(to, from, next) {
    this.show = true
    next()
  },
  data() {
    return {
      area: '内地',
      areaData: [
        { id: 0, name: '内地' },
        { id: 1, name: '港台' },
        { id: 2, name: '欧美' },
        { id: 3, name: '日本' },
        { id: 4, name: '韩国' },
      ],
      data: null,
      id: 0,
      loading: false,
      show: false,
    };
  },
  methods: {
    ...mapActions(['saveMVurl', 'saveResMv', 'saveMvBes', 'saveArtistsItem']),
    // 标签切换
    changeTag(e) {
      this.id = e.id;
      this.area = e.name;
      this.getData();
    },

    // 获取数据
    async getData() {
      this.$api.mvRank({ limit: 50, area: this.area }).then((ret) => {
        this.data = ret.data;
      });
    },

    // 播放MV
    async playMv(e) {
      // 获取到MV的地址
      this.$api.mvUrl({ id: parseInt(e.id), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMVurl({ mvUrl: ret.data.url });
      });
      this.getArtists(e.artistId);
      this.getResMv(e.id);
      this.getItem(e.id);
    },

    // 获取相似MV
    async getResMv(e) {
      this.$api.simiMv({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveResMv({ resMv: ret });
      });
    },

    // 获取MV基础信息
    async getItem(e) {
      this.loading = true;
      this.$api.mvDetail({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMvBes({ mvBes: ret });
        this.$router.push(`/mvPlay?id=${e}`);
      });
    },

    // 获取MV作者
    async getArtists(e) {
      window.sessionStorage.setItem('artists', e);
      this.$api.arDetail({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveArtistsItem({ artistsItem: ret.data });
      });
    },

    back() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.body {
  margin-bottom: 40px;
}

.top {
  display: flex;
  align-items: center;
  font-size: 20px;
  margin-bottom: 20px;
}

.back {
  font-size: 30px;
}

.tagBox {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.tag {
  display: flex;
}

.tag div {
  margin-right: 10px;
}

.content {
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
}

.contentBox {
  width: 48%;
  height: 188px;
  margin-bottom: 20px;
  margin-right: 10px;
  position: relative;
  display: flex;
  justify-content: left;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.count {
  position: absolute;
  z-index: 10;
  text-align: right;
  color: #fff;
  right: 0;
}

.desc {
  height: 188px;
  width: 196px;
  margin-left: 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.name {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.oneword {
  width: 100%;
  overflow: hidden;
  white-space: normal;
  margin-bottom: 15px;
}

.card {
  margin-bottom: 10px;
  cursor: pointer;
  position: relative;
}

.card .el-image {
  width: 335px;
  height: 188px;
}

.rank {
  font-size: 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-right: 20px;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}
</style>
